﻿module Details

open System.Net
open System.Text.RegularExpressions

/// Gets a header value from an array of Web response headers.    
let getHeader headers header =
    let h = headers |> Array.tryFind (fun x -> fst x = header)
    match h with
    | Some (_, value) -> value
    | None -> ""

/// Matches a pattern in an HTML string and interprets special characters within the match value.
let getElement pattern html = Regex(pattern).Match(html)
                                            .Groups
                                            .[1]
                                            .Value
                                            .Trim() 
                                            |> WebUtility.HtmlDecode

/// Regex patterns for the title, meta description, meta keywords and h1 tags.
let patterns = 
    [
        "(?is)<title>(.*)</title>"
        "(?is)<meta\ +name=\"description\"\ +content=\"([^/\"]*)\""
        "(?is)<meta\ +name=\"keywords\"\ +content=\"([^/\"]*)\""
        "(?is)<h1>(.*?)</h1>"
    ]